<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 处理接收回复消息
// +----------------------------------------------------------------------
namespace app\ymkj\controller;

use EasyWeChat\Foundation\Application;
use app\model\Order as orderModel;
use EasyWeChat\Payment\Order;
use think\facade\Session;
use think\facade\Config;
use think\Controller;
use think\Db;

class User extends Controller
{
  // 个人信息
  public function index()
  {
    $options = Config::get('wewhat.wxConfig');
    $app = new Application($options);
    $oauth = $app->oauth;
    // 未登录
    if (empty(Session::get('wewhat_user'))) {
      Session::set('target_url','User/index');
      return $oauth->redirect();
    }
    // 已经登录过
    $user = Session::get('wewhat_user');
    $this->assign([
      'user' => $user
    ]);
    return view();
  }

  // 填写信息
  public function info()
  {
    $user = Session::get('wewhat_user');
    if($this->request->isPost()) {
      $post = input('post.');
      $result = Db::name('user')->where('openid','=',$user['openid'])->update($post);
      if ($result === false) {
        $this->error('添加失败');
      }
      else {
        $this->success('添加成功','user/xufei');
      }
    } else {
      $huiyuan = Db::name('user')->where('openid','=',$user['openid'])->find();
      $this->assign('huiyuan',$huiyuan);
      return view();
    }
  }

  // 会员
  public function huiyuan()
  {
    $user = Session::get('wewhat_user');
    $yonghu = Db::name('user')->where('openid','=',$user['openid'])->find();
    if( $yonghu['vip'] == 1 ){
      $this->assign('yonghu',$yonghu);
      return view();
    } else {
      return $this->redirect('user/info');
    }
  }

  // 修改会员信息
  public function eidthui()
  {
    $user = Session::get('wewhat_user');
    if($this->request->isPost()){
      $post = input('post.');
      $res = Db::name('user')->where('openid','=',$user['openid'])->update($post);
      if( $res ){
        $this->success('修改成功','user/huiyuan');
      } else {
        $this->error('修改失败');
      }
    } else {
      $huiyuan = Db::name('user')->where('openid','=',$user['openid'])->find();
      $this->assign('huiyuan',$huiyuan);
      return view();
    }
  }

   // 订单信息
   public function order()
   {
     $user = Session::get('wewhat_user');
     $order = orderModel::with(['xiche','user'])
      ->where('openid','=',$user['openid'])
      ->order('create_time desc')
      ->limit(20)
      ->select();
     $this->assign([
       'order' => $order
     ]);
     return view();
   }
  
  // 我的钱包
  public function qianbao()
  {
    $user = Session::get('wewhat_user');
    $balance = Db::name('user')->where('openid','=',$user['openid'])->value('balance');
    $tixian = Db::name('tixian')->where('openid','=',$user['openid'])->order('create_time desc')->select();
    $this->assign([
      'balance' => $balance,
      'tixian' => $tixian
    ]);
    return view();
  }

  // 提现
  public function tixian()
  {
    $user = Session::get('wewhat_user');
    if($this->request->isPost()){
      $post = input('post.');
      $balance = Db::name('user')->where('openid','=',$user['openid'])->value('balance');
      if( !$balance || $balance < $post['price']){
        $this->error('余额不足');
      }
      if($post['price']%10 != 0){
        $this->error('提现金额为10的整倍数');
      }
      if($post['price'] < 10){
        $this->error('提现金额为10的整倍数');
      }
      Db::name('user')->where('id','=',$post['id'])
        ->update([
          'txname' => $post['txname'],
          'txzhanghu' => $post['txzhanghu'],
          'txyinhang' => $post['txyinhang'],
        ]);
      Db::startTrans();
      $tixian = Db::name('tixian')->insert([
        'price' => $post['price'],
        'openid' => $user['openid'],
        'create_time' => time(),
      ]);
      $yue = Db::name('user')->where('id','=',$post['id'])
        ->update([
          'balance' => $balance - $post['price'],
        ]);
      Db::commit();
      if( $tixian && $yue ){
        $this->success('提交成功','user/qianbao');
      } else {
        Db::rollback();
        $this->error('提交失败');
      }
    } else {
      $info = Db::name('user')->where('openid','=',$user['openid'])->find();
      $this->assign([
        'info' => $info,
      ]);
      return view();
    }
  }

  // 推广码
  public function qurcode()
  {
    $config = Config::get('wewhat.wxConfig');
    $user = request()->param();
    $vip = Db::name('user')->where('openid','=',$user['openid'])->value('vip');
    if ( $vip ) {
      $app = new Application($config);
      $qrcode = $app->qrcode;
      $result = $qrcode->temporary($user['openid'],30 * 24 * 3600);
      $ticket = $result->ticket; 
      $url = $qrcode->url($ticket);
      $this->assign('url',$url);
      return view();
    } else {
      return '<div style="font-size:38px;text-align:center;line-height:200px;">请您先开通会员</div>';
    }
  }

  // 我的团队
  public function tuandui()
  {
    $user = Session::get('wewhat_user');
    $f1 = Db::name('user')->where('f1','=',$user['openid'])->count('id');
    $f2 = Db::name('user')->where('f2','=',$user['openid'])->count('id');
    $f3 = Db::name('user')->where('f3','=',$user['openid'])->count('id');
    $counts = $f1 + $f2 + $f3;
    $huiyuan = Db::name('user')
      ->where('f1','=',$user['openid'])
      ->where('vip','=',1)
      ->select();
    $this->assign([
      'f1' => $f1,
      'counts' => $counts,
      'huiyuan' => $huiyuan
    ]);
    return view();
  }

  // 续费
  public function xufei()
  {
    $xufei = Db::name('shops')->find();
    $this->assign('xufei',$xufei);
    return view();
  }

  // 下单
  public function xiadan()
  {
    $order = new orderModel();
    if($this->request->isPost()){
      $user = Session::get('wewhat_user');
      $shop = Db::name('shops')->find();
      $createOrder = $order->save([
        'openid' => $user['openid'],
        'name' => "会员充值",
        'type' => 'more',
        'jine' => $shop['price'],
      ]);
      if( $createOrder ){
        return redirect('Buy/index',array('order_id'=>$order->id));
      } else {
        $this->error('下单失败');
      }
    }
  }

}
?>